package com.xuchen.zhenyang.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.OrderBase;
import com.xuchen.zhenyang.entity.OrderGoods;
import com.xuchen.zhenyang.entity.User;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.enums.DeliveryTypeEnum;
import com.xuchen.zhenyang.enums.PayTypeEnum;
import com.xuchen.zhenyang.enums.StatusEnum;
import com.xuchen.zhenyang.enums.UserTypeEnums;
import com.xuchen.zhenyang.service.OrderBaseService;
import com.xuchen.zhenyang.service.OrderGoodsService;
import com.xuchen.zhenyang.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("orderBase")
@RequiresPermissions("orderBase")
public class OrderBaseController extends BaseController {


    @Autowired
    OrderBaseService orderBaseService;
    @Autowired
    OrderGoodsService orderGoodsService;
    @Autowired
    UserService userService;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        return "order/base/order-list";
    }

    @GetMapping("list")
    @ResponseBody
    Result list(BaseQuery baseQuery, OrderBase myEntity, String params, HttpServletRequest request) {
        if (StrUtil.isNotBlank(params)) {
            myEntity = JSONObject.parseObject(params).toJavaObject(OrderBase.class);
        }
        MyEntityWrapper<OrderBase> wrapper = new MyEntityWrapper<>(baseQuery, myEntity);
        wrapper.eq("customer_id").eq("order_type").eq("delivery_type").eq("delivery_user_id")
                .eq("pay_type").timeBetween("order_time");
        List<OrderBase> list = orderBaseService.selectList(wrapper);
        return Result.success(PageHelper.freeTotal(), list);
    }

    @PostMapping("editText")
    @ResponseBody
    @RequestLog
    Result editText(OrderBase myEntity) {
        myEntity.setUpdateTime(new Date());
        myEntity.setUpdateUser(getSessionUserName());
        orderBaseService.updateById(myEntity);
        return Result.success();
    }

    @GetMapping("toAdd")
    String toAdd(HttpServletRequest request) {
        setAttributeEnums(request);
        return "order/base/order-add";
    }

    @PostMapping("doAdd")
    @ResponseBody
    @RequestLog
    Result doAdd(OrderBase myEntity) {
        myEntity.setCreateUser(getSessionUserName());
        orderBaseService.insert(myEntity);
        return Result.success();
    }

    @GetMapping("toEdit")
    String toEdit(OrderBase myEntity, HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("myEntity", orderBaseService.selectById(myEntity));
        return "order/base/order-edit";
    }

    @PostMapping("doEdit")
    @ResponseBody
    @RequestLog
    Result doEdit(OrderBase myEntity) {
        myEntity.setUpdateTime(new Date());
        myEntity.setUpdateUser(getSessionUserName());
        orderBaseService.updateById(myEntity);
        return Result.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @RequestLog
    Result delete(OrderBase myEntity) {
        orderBaseService.deleteById(myEntity);
        orderBaseService.updateGoodsCountForDel(myEntity.getOrderBaseId());
        orderGoodsService.delete(new EntityWrapper<OrderGoods>().eq("order_base_id", myEntity.getOrderBaseId()));
        return Result.success();
    }

    private void setAttributeEnums(HttpServletRequest request) {
        List<User> deliverList = userService.selectList(new EntityWrapper<User>()
                .eq("user_type", UserTypeEnums.OTHER.getId()).or().eq("user_type", UserTypeEnums.DELIVER.getId())
                .andNew().eq("status", StatusEnum.NORMAL.getId()).orderBy("user_name"));
        List<User> customerList = userService.selectList(new EntityWrapper<User>().eq("status", StatusEnum.NORMAL.getId()).orderBy("user_name"));
        request.setAttribute("customerList", customerList);
        request.setAttribute("deliverList", deliverList);
        request.setAttribute("deliveryTypeEnum", DeliveryTypeEnum.getMap());
        request.setAttribute("payTypeEnum", PayTypeEnum.getMap());
    }
}
